﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chowpoo.Common;
using Chowpoo.Util;
using CourseSystem.Common;
using CourseSystem.Common.Helpers;
using CourseSystem.Core.BLL;
using CourseSystem.Core.Entities;
using CourseSystem.Core.Enum;
using CourseSystem.Stat.ActionFilter;
using Webdiyer.WebControls.Mvc;

namespace CourseSystem.Web.Controllers
{
    [UserAuth(UserType = UserTypeEnum.Teacher)]
    public class AdminMonitorController : BaseController
    {
        [UrlStatFilter]
        public ActionResult Student()
        {
            return View();
        }

        [UserAuth(UserType = UserTypeEnum.Admin)]
        public ActionResult Teacher()
        {
            return View();
        }

        public ActionResult StudentListUc(int? pid, int? collegeIdSearch, int? departIdSearch, int? subjectIdSearch, int? yearIdSearch, int? classIdSearch, string uidSearch, string nameSearch)
        {
            CommonRequest.ClearCache();
            var list = UrlStatBLL.GetList(collegeIdSearch.CheckInt(0), departIdSearch.CheckInt(0), subjectIdSearch.CheckInt(0),
               yearIdSearch.CheckInt(0), classIdSearch.CheckInt(0), uidSearch, nameSearch).OrderByDescending(o => o.Id);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }
        [UserAuth(UserType = UserTypeEnum.Admin)]
        public ActionResult TeacherListUc(int? pid, string uidSearch, string nameSearch, int? type)
        {
            type = type.CheckInt(-1);
            CommonRequest.ClearCache();
            var list = UrlStatBLL.GetList(uidSearch, nameSearch, type.Value).OrderByDescending(o => o.Id);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }

        [UserAuth(UserType = UserTypeEnum.Admin)]
        public ActionResult TeacherHoursListUc(int? pid, string uidSearch, string nameSearch, int? type)
        {
            type = type.CheckInt(-1);
            CommonRequest.ClearCache();
            var list = TeacherBLL.GetList(uidSearch, nameSearch, type.Value).OrderByDescending(o => o.Id);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }

        [UrlStatFilter]
        public ActionResult StudentHours()
        {
            return View();
        }
        [UserAuth(UserType = UserTypeEnum.Admin)]
        public ActionResult TeacherHours()
        {
            return View();
        }

        public ActionResult StudentHoursEditUc()
        {
            var oldInfo = CourseSettingBLL.GetFromDb();
            if (oldInfo == null)
                return NotFound();
            return PartialView(oldInfo);
        }
        [HttpPost]
        public ActionResult StudentHoursEditUc(CourseSettingInfo info)
        {
            ViewData.Model = info;
            var oldInfo = CourseSettingBLL.GetFromDb();
            if (oldInfo == null)
                return NotFound();
            if (info.StudentHours == 0)
            {
                ModelState.AddModelError("StudentHours", "请填写有效时数!");
                return PartialView();
            }
            oldInfo.StudentHours = info.StudentHours;
            if (CourseSettingBLL.Edit(oldInfo))
            {
                SetHighLineMsg("保存成功.");
                return PartialView(oldInfo);
            }
            SetHighLineMsg("未知错误.");
            return PartialView(info);
        }

        [UserAuth(UserType = UserTypeEnum.Admin)]
        public ActionResult TeacherHoursEditUc()
        {
            var oldInfo = CourseSettingBLL.GetFromDb();
            if (oldInfo == null)
                return NotFound();
            return PartialView(oldInfo);
        }
        [HttpPost]
        [UserAuth(UserType = UserTypeEnum.Admin)]
        public ActionResult TeacherHoursEditUc(CourseSettingInfo info)
        {
            ViewData.Model = info;
            var oldInfo = CourseSettingBLL.GetFromDb();
            if (oldInfo == null)
                return NotFound();
            if (info.TeacherHours == 0)
            {
                ModelState.AddModelError("TeacherHours", "请填写有效时数!");
                return PartialView();
            }
            oldInfo.TeacherHours = info.TeacherHours;
            if (CourseSettingBLL.Edit(oldInfo))
            {
                SetHighLineMsg("保存成功.");
                return PartialView(oldInfo);
            }
            SetHighLineMsg("未知错误.");
            return PartialView(info);
        }

        public ActionResult StudentHoursListUc(int? pid, int? collegeIdSearch, int? departIdSearch, int? subjectIdSearch, int? yearIdSearch, int? classIdSearch, string uidSearch, string nameSearch)
        {
            CommonRequest.ClearCache();
            var list = StudentBLL.GetList(collegeIdSearch.CheckInt(0), departIdSearch.CheckInt(0), subjectIdSearch.CheckInt(0), classIdSearch.CheckInt(0), yearIdSearch.CheckInt(0), uidSearch, nameSearch).OrderByDescending(o => o.Id);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }

    }
}
